//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Platforms/Base/IApplication.h"

#if defined( PLATFORM_SDL )

#include "Platforms/SDL/SDLKeyCodes.h"
#include "Platforms/SDL/SDLFileSystem.h"
#include "Platforms/SDL/SDLOpenGL.h"

#include "Base/Input/Input.h"
#include "Base/Types/Logger.h"


namespace Engine
{
namespace Platforms
{

	//
	// SDL Base Application
	//

	class SDLApplication : public IApplication
	{
	// variables
	private:
		ParallelThreadManager	_threadManager;
		EventSystem				_events;
		SDLFileSystem			_filesystem;
		CurrentThread			_thread;
		Input					_input;
		Logger					_logger;
		ThreadMessageQueue		_messageQueue;


	// methods
	public:
		SDLApplication (IPlatform *platform, EDevice::type deviceType = EDevice::AUTO) :
			IApplication( platform ), _threadManager( ESS() ),
			_events( ESS() ), _filesystem( ESS() ), _input( ESS() ),
			_logger( ESS() )
		{
			_platform->InitDevice( deviceType );
		}

		~SDLApplication ()
		{
			_events.Clear();

			_GetLogger()->Close();
			_GetLogger()->CloseCrashFile();
		}


	protected:
		Logger *	_GetLogger ()		{ return &_logger; }
		

	// IApplication interface
	private:
		virtual void Destroy ()	override						{ delete this; }


	// ParallelThread interface
	public:

		virtual bool IsCurrent () const override
		{
			return _thread.IsCurrent();
		}

		virtual void Flush () override
		{
			ASSERT( IsCurrent() );
			_messageQueue.Flush();
		}
		
		virtual void Push (IParallelOp *op) override
		{
			_messageQueue.Push( op );
		}

		virtual void Quit () override
		{
			_platform->Terminate();
		}
		
		virtual uint ProcessMessages () override
		{
			return _messageQueue.ProcessAll( this );
		}
	};


}	// Platforms


namespace Base
{
	typedef Platforms::IPlatform			IPlatform;
	typedef Platforms::SDLApplication		Application;
	typedef Platforms::EKey					EKey;
	typedef Platforms::ESensor				ESensor;
	typedef Platforms::HardwareCamera		HardwareCamera;

}	// Base
}	// Engine

#endif	// PLATFORM_IPHONE